package com.dahua.messagesyncsh.rabbitmq.consumer;

import com.dahua.messagesyncsh.rabbitmq.RabbitmqUtil;
import com.dahua.messagesyncsh.rabbitmq.enums.Rabbitmq;
import com.dahua.messagesyncsh.rabbitmq.messageListener.HitachiMessageListener;
import com.dahua.messagesyncsh.rabbitmq.messageListener.MitsubishiMessageListener;
import com.raizumi.common.rabbitmq.JoinstecRabbitmqTemplate;
import com.raizumi.common.rabbitmq.RabbitmqAdminister;
import com.raizumi.common.rabbitmq.entity.Publisher;
import com.raizumi.common.rabbitmq.entity.SimpleMLC;
import com.raizumi.common.utils.tool.ContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Properties;

@Slf4j
@Component
public class Hitachi485Runnable implements Runnable {
    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private String port;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    @Value("${spring.rabbitmq.hitachi.queue}")
    private String queue;

    @Value("${spring.rabbitmq.hitachi.exchange}")
    private String exchange;

    @Value("${spring.rabbitmq.hitachi.routing-key}")
    private String routingKey;

    @Value("${mitsubishi.factory.host}")
    private String mitsubishiHost;

    @Value("${mitsubishi.factory.port}")
    private Integer mitsubishiPort;

    @Value("${mitsubishi.factory.username}")
    private String mitsubishiUsername;

    @Value("${mitsubishi.factory.password}")
    private String mitsubishiPassword;

    @Resource
    private RabbitmqUtil rabbitmqUtil;

    @Resource
    private HitachiMessageListener hitachiMessageListener;

    @Resource
    private MitsubishiMessageListener mitsubishiMessageListener;

    @Resource
    private RabbitmqAdminister rabbitmqAdminister;

    @Resource
    private ContextUtil contextUtil;

    @Override
    public void run() {

        /*  消费日立数据*/
        rabbitmqUtil.create(queue, exchange, routingKey);

        Properties properties = new Properties();
        properties.put(Rabbitmq.HOST.getName(), host);
        properties.put(Rabbitmq.PORT.getName(), port);
        properties.put(Rabbitmq.USERNAME.getName(), username);
        properties.put(Rabbitmq.PASSWORD.getName(), password);
        properties.put(Rabbitmq.QUEUE.getName(), queue);
        properties.put(Rabbitmq.PRE_FETCH_COUNT.getName(), "10");
        properties.put(Rabbitmq.CONCURRENT_CONSUMERS.getName(), "1");
        properties.put(Rabbitmq.MAX_CONCURRENT_CONSUMERS.getName(), "1");
        properties.put(Rabbitmq.DEFAULT_REQUEUE_REJECTED.getName(), "false");
        properties.put(Rabbitmq.ENABLE_BATCH.getName(), "true");
        properties.put(Rabbitmq.BATCH_SIZE.getName(), "100");

        boolean registered = rabbitmqUtil.registerConsumer(properties, hitachiMessageListener, null, null);
        if (registered) {
            rabbitmqUtil.startConsumer(queue);
            log.info("【rabbitmq】：启动日立485数据消费服务成功");
        }

        CachingConnectionFactory factory = new CachingConnectionFactory();

        factory.setHost(mitsubishiHost);
        factory.setPort(mitsubishiPort);  // RabbitMQ 默认端口
        factory.setUsername(mitsubishiUsername);
        factory.setPassword(mitsubishiPassword);
        factory.setVirtualHost("/"); // 虚拟主机

        // 3. 配置缓存
        factory.setChannelCacheSize(25); // 缓存 25 个 channel

        Publisher publisher = new Publisher(factory);

        JoinstecRabbitmqTemplate joinstecRabbitTemplate = rabbitmqAdminister.buildTemplate(publisher);
        contextUtil.register("joinstecRabbitTemplate", JoinstecRabbitmqTemplate.class, joinstecRabbitTemplate);

        SimpleMLC simpleMLC = new SimpleMLC();
        simpleMLC.setQueueNames(new String[]{"TEST"});
        simpleMLC.setMessageListener(mitsubishiMessageListener);
        simpleMLC.setConsumerBatchEnabled(true);
        simpleMLC.setBatchSize(100);
        joinstecRabbitTemplate.simpleSubscribe(simpleMLC);
    }
}
